//
//  TestNavBarLayoutConstraint.m
//  MKUITestApp
//
//  Created by 苏尚进 on 2025/4/7.
//  Copyright © 2025 moon. All rights reserved.
//

#import "TestNavBarLayoutConstraint.h"
#import "Menu.h"
#import "MKUIElementListViewCellSelection+Test.h"
#import "MKUIElementListViewCellSwitch+Test.h"
#import "TestElementHeaders.h"
@interface TestNavBarView : UIView
@property(nonatomic,strong) MKUINavigationBarLayoutConstraint *navlayout;
@end
@implementation TestNavBarView
- (id)initWithFrame:(CGRect)frame{
    if(self=[super initWithFrame:frame]){
        self.navlayout = [[MKUINavigationBarLayoutConstraint alloc] init];
        self.navlayout.contentInsets = MKUIEdgeInsetsMakeSameEdge(10);
        self.navlayout.interitemSpacing = 10;
    }
    return self;
}
- (void)layoutSubviews{
    [super layoutSubviews];
    CGRect bounds = self.bounds;
    CGRect f1 = bounds;
    f1.size = [self.navlayout sizeThatFits:bounds.size resizeItems:YES];
    MKCGRectAlignMidXToRect(&f1, bounds);
    self.navlayout.bounds = f1;
    [self.navlayout layoutItemsWithResizeItems:YES];
}
- (CGSize)sizeThatFits:(CGSize)size{
    CGSize s = [self.navlayout sizeThatFits:size resizeItems:YES];
    return s;
}
@end

@interface TestNavBarLayoutConstraint ()
@property(nonatomic,strong) TestNavBarView *navBarElement;
@property(nonatomic,strong) MKUIElementListViewCellButton *addLeftButton;
@property(nonatomic,strong) MKUIElementListViewCellButton *addCenterButton;
@property(nonatomic,strong) MKUIElementListViewCellButton *addRightButton;

@end

@implementation TestNavBarLayoutConstraint

+ (MKTFuncItem *)funcItem{
    return [MKTFuncItem funcWithName:@"NavBar布局约束" viewControllerClass:self];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self reloadData];
}
- (void)reloadData{
    @MK_WEAKIFY(self);
    [self.listView deleteAllSections];
    [self.listView addSection];
    [self.listView addItem:self.navBarElement];
    
    [self.listView addSection];
    [self.listView addItem:[MKUIElementListViewCellSelection testElementMKUILayoutConstraintDirection:@"layoutDirection" selected:self.navBarElement.navlayout.layoutDirection whenDone:^(MKUILayoutConstraintDirection selectedValue) {
        @MK_NORMALIZE(self);
        self.navBarElement.navlayout.layoutDirection = selectedValue;
        [self.navBarElement mk_reloadInListView];
    }]];
    [self.listView addItem:[MKUIElementListViewCellSelection testElementMKUILayoutConstraintVerticalAlignment:@"layoutVerticalAlignment" selected:self.navBarElement.navlayout.layoutVerticalAlignment whenDone:^(MKUILayoutConstraintVerticalAlignment selectedValue) {
        @MK_NORMALIZE(self);
        self.navBarElement.navlayout.layoutVerticalAlignment = selectedValue;
        [self.navBarElement mk_reloadInListView];
    }]];
    
    [self.listView addItem:[TestMKUIElementViewSlider_UIEdgeInsets testSliderElementWithTitle:@"contentInsets" value:self.navBarElement.navlayout.contentInsets minimumValue:UIEdgeInsetsZero maximumValue:MKUIEdgeInsetsMakeSameEdge(20) whenChange:^(TestMKUIElementViewSlider_UIEdgeInsets * _Nonnull el, UIEdgeInsets value) {
        @MK_NORMALIZE(self);
        self.navBarElement.navlayout.contentInsets = value;
        [self.navBarElement mk_reloadInListView];
    }]];
    [self.listView addItem:[TestMKUIElementViewSlider testSliderElementWithTitle:@"interitemSpacing" value:self.navBarElement.navlayout.interitemSpacing minimumValue:0 maximumValue:20 whenChange:^(TestMKUIElementViewSlider * _Nonnull el, CGFloat value) {
        @MK_NORMALIZE(self);
        self.navBarElement.navlayout.interitemSpacing = value;
        [self.navBarElement mk_reloadInListView];
    }]];
    [self.listView addItem:[MKUIElementListViewCellSwitch testSwitchElementWithTitle:@"fixSizeToFitsBounds" on:self.navBarElement.navlayout.fixSizeToFitsBounds whenChange:^(BOOL on) {
        @MK_NORMALIZE(self);
        self.navBarElement.navlayout.fixSizeToFitsBounds = on;
        [self.navBarElement mk_reloadInListView];
    }]];
    
    [self.listView addSection];
    [self.listView addItem:self.addLeftButton];
    [self.listView addItem:self.addCenterButton];
    [self.listView addItem:self.addRightButton];
    
    [self.listView reloadData];
}
- (TestNavBarView *)navBarElement{
    if(_navBarElement) return _navBarElement;
    _navBarElement = [[TestNavBarView alloc] init];
    return _navBarElement;
}
- (MKUIElementListViewCellButton *)addLeftButton{
    if(_addLeftButton) return _addLeftButton;
    MKUIElementListViewCellButton *button = [[MKUIElementListViewCellButton alloc] init];
    _addLeftButton = button;
    button.elementView.titleText = @"添加左侧按钮";
    @MK_WEAKIFY(self);
    button.whenClick = ^(__kindof MKUIListSelectionCellViewBase * _Nonnull el) {
        @MK_NORMALIZE(self);
        MKUIFlowLayoutButton *barButton = [[MKUIFlowLayoutButton alloc] init];
        barButton.imageSize = CGSizeMake(40, 40);
        [barButton setImage:Menu.randomImage forState:(UIControlStateNormal)];
        @MK_WEAKIFY(barButton);
        [barButton mk_addClickActionBlock:^(TestNavBarLayoutConstraint *self) {
            @MK_NORMALIZE(barButton);
            [barButton removeFromSuperview];
            [self.navBarElement.navlayout removeHeadItem:barButton];
            [self.navBarElement mk_reloadInListView];
        } context:self];
        [self.navBarElement addSubview:barButton];
        [self.navBarElement.navlayout addHeadItem:barButton];
        [self.navBarElement mk_reloadInListView];
    };
    return _addLeftButton;
}
- (MKUIElementListViewCellButton *)addCenterButton{
    if(_addCenterButton) return _addCenterButton;
    MKUIElementListViewCellButton *button = [[MKUIElementListViewCellButton alloc] init];
    _addCenterButton = button;
    button.elementView.titleText = @"添加中侧按钮";
    @MK_WEAKIFY(self);
    button.whenClick = ^(__kindof MKUIListSelectionCellViewBase * _Nonnull el) {
        @MK_NORMALIZE(self);
        MKUIFlowLayoutButton *barButton = [[MKUIFlowLayoutButton alloc] init];
        barButton.imageSize = CGSizeMake(30, 30);
        barButton.titleLabel.minimumScaleFactor = 0.5;
        barButton.titleLabel.numberOfLines = 2;
        barButton.titleLabel.adjustsFontSizeToFitWidth = YES;
        barButton.titleLabel.textAlignment = NSTextAlignmentCenter;
        [barButton setTitleColor:UIColor.blackColor forState:(UIControlStateNormal)];
        [barButton setTitle:@"导航栏标题" forState:(UIControlStateNormal)];
//        [barButton setImage:Menu.randomImage forState:(UIControlStateNormal)];
        @MK_WEAKIFY(barButton);
        [barButton mk_addClickActionBlock:^(TestNavBarLayoutConstraint *self) {
            @MK_NORMALIZE(barButton);
            [barButton removeFromSuperview];
            [self.navBarElement.navlayout removeCenterItem:barButton];
            [self.navBarElement mk_reloadInListView];
        } context:self];
        [self.navBarElement addSubview:barButton];
        [self.navBarElement.navlayout addCenterItem:barButton];
        [self.navBarElement mk_reloadInListView];
    };
    return _addCenterButton;
}
- (MKUIElementListViewCellButton *)addRightButton{
    if(_addRightButton) return _addRightButton;
    MKUIElementListViewCellButton *button = [[MKUIElementListViewCellButton alloc] init];
    _addRightButton = button;
    button.elementView.titleText = @"添加右侧按钮";
    @MK_WEAKIFY(self);
    button.whenClick = ^(__kindof MKUIListSelectionCellViewBase * _Nonnull el) {
        @MK_NORMALIZE(self);
        MKUIFlowLayoutButton *barButton = [[MKUIFlowLayoutButton alloc] init];
        barButton.imageSize = CGSizeMake(20, 20);
        [barButton setImage:Menu.randomImage forState:(UIControlStateNormal)];
        @MK_WEAKIFY(barButton);
        [barButton mk_addClickActionBlock:^(TestNavBarLayoutConstraint *self) {
            @MK_NORMALIZE(barButton);
            [barButton removeFromSuperview];
            [self.navBarElement.navlayout removeFootItem:barButton];
            [self.navBarElement mk_reloadInListView];
        } context:self];
        [self.navBarElement addSubview:barButton];
        [self.navBarElement.navlayout addFootItem:barButton];
        [self.navBarElement mk_reloadInListView];
    };
    return _addRightButton;
}
@end
